package com.wg.msshopsp.backendmanagement.shop.controller;

import com.wg.msshopsp.backendmanagement.shop.entity.ShopP;
import com.wg.msshopsp.backendmanagement.shop.service.ShopService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Tag(name = "店铺相关接口", description = "查询店铺及商品、规格信息")
public class ShopPController {

    @Autowired
    private ShopService shopService; // 注入Service

    @GetMapping("/shop/with-products")
    @Operation(summary = "根据用户ID查询店铺及关联的商品、规格、规格值")
    public ResponseEntity<List<ShopP>> getShopsWithProducts(
            @Parameter(description = "用户ID（店家ID）", required = true)
            @RequestParam Integer userId
    ) {
        // 调用Service获取数据
        List<ShopP> shops = shopService.getShopsWithProductsByUserId(userId);
        // 返回200 OK及数据
        return ResponseEntity.ok(shops);
    }



    // 新增接口：新增店铺
    @PostMapping("/shop/shopMS")
    @Operation(summary = "新增店铺", description = "创建新店铺，需指定用户ID和店铺名称")
    public ResponseEntity<Map<String, Object>> addShop(
            @Parameter(description = "店铺信息（包含userId、shopName等）", required = true)
            @RequestBody ShopP shop
    ) {
        Integer shopId = shopService.addShop(shop);
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "店铺创建成功");
        response.put("data", shopId); // 返回新增的店铺ID
        return ResponseEntity.ok(response);
    }

    // 新增接口：更新店铺
    @PutMapping("/shop/shopMS")
    @Operation(summary = "更新店铺信息", description = "支持更新店铺名称、描述、状态、logo等")
    public ResponseEntity<Map<String, Object>> updateShop(
            @Parameter(description = "店铺信息（必须包含id）", required = true)
            @RequestBody ShopP shop
    ) {
        shopService.updateShop(shop);
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "店铺更新成功");
        response.put("data", shop.getId());
        return ResponseEntity.ok(response);
    }

    // 新增接口：删除店铺
    @DeleteMapping("/shop/shopMS/{id}")
    @Operation(summary = "删除店铺", description = "根据店铺ID删除店铺（需先确保无关联商品或设置级联删除）")
    public ResponseEntity<Map<String, Object>> deleteShop(
            @Parameter(description = "店铺ID", required = true)
            @PathVariable Integer id
    ) {
        shopService.deleteShop(id);
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "店铺删除成功");
        return ResponseEntity.ok(response);
    }

    // 新增接口：根据ID查询店铺详情
    @GetMapping("/shop/shopMS/{id}")
    @Operation(summary = "根据ID查询店铺详情", description = "查询单个店铺的基本信息（不含商品）")
    public ResponseEntity<ShopP> getShopById(
            @Parameter(description = "店铺ID", required = true)
            @PathVariable Integer id
    ) {
        ShopP shop = shopService.getShopById(id);
        return ResponseEntity.ok(shop);
    }


    //================================
    @GetMapping("/shop/with-products/condition")
    @Operation(summary = "根据用户ID及商品条件查询店铺及关联的商品、规格、规格值",
            description = "支持通过商品名、商品类型、商品状态进行模糊查询")
    public ResponseEntity<List<ShopP>> getShopsWithProductsByCondition(
            @Parameter(description = "用户ID（店家ID）", required = true)
            @RequestParam Integer userId,
            @Parameter(description = "商品名称（模糊查询）")
            @RequestParam(required = false) String productName,
            @Parameter(description = "商品类型（模糊查询）")
            @RequestParam(required = false) String productType,
            @Parameter(description = "商品状态（模糊查询）")
            @RequestParam(required = false) String productStatus
    ) {
        // 调用Service获取带条件过滤的数据
        List<ShopP> shops = shopService.getShopsWithProductsByCondition(userId, productName, productType, productStatus);
        return ResponseEntity.ok(shops);
    }
}
